package cn.tedu.logger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.log4j.MDC;
import org.apache.log4j.NDC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestLogger {
    private static Logger logger = LoggerFactory.getLogger(TestLogger.class);

    public static void main(String[] args) {
        MDC.put("user", "zhangsan");

        logger.debug("main函数开始运行");

        // 模拟开启线程访问API接口
        new Thread(new Runnable() {

            @Override
            public void run() {
                getUser();
            }
        }).start();

        try {
            Thread.sleep(1 * 100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        MDC.remove("user");
        logger.debug("main函数结束运行");
    }

    /**
     * 假设这是一个API的处理(进入此环境，我们向NDC存储一个访问的IP)
     */
    public static void getUser() {
        NDC.push("127.0.0.1");
        logger.debug("访问接口");

        logger.debug("NDC.get(): {} ", NDC.get());
        // 移除
        // NDC.pop();

        // 适当位置清空
        NDC.remove();
        logger.debug("remove之后 NDC.get(): {} ", NDC.get());
    }

}
